package com.smcaiot.core.gateway.north;

import com.smcaiot.core.exception.ApplicationDomainException;
import com.smcaiot.core.exception.ApplicationInfrastructureException;
import com.smcaiot.core.exception.ApplicationValidationException;
import org.springframework.http.HttpStatus;

import java.io.Serializable;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 微服务调用响应
 */
public class RestResp<T> implements Serializable {
    private static Logger logger = Logger.getLogger(RestResp.class.getName());
    /**
     * 请求成功
     */
    public static final String CODE_SUCCESS = "200";
    /**
     * 失败
     */
    public static final String CODE_FAIL_B = "9999";
    /**
     * 没有访问权限
     */
    public static final String CODE_FAIL_A = "10000";
    /**
     * token认证失败
     */
    public static final String CODE_FAIL_T = "10001";
    /**
     * 参数格式不正确
     */
    public static final String CODE_FAIL_P = "10002";
    /**
     * 请求频次超过限额
     */
    public static final String CODE_FAIL_O = "10003";
    /**
     * 用户未注册
     */
    public static final String CODE_FAIL_N = "10004";
    /**
     * 业务数据
     */
    private T data;

    /**
     * 状态码
     */
    private String code;

    /**
     * 消息
     */
    private String msg;

    /**
     * 详细信息
     */
    private String details;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    /**
     * 通用服务调用成功
     *
     * @param data
     * @return
     */
    public static RestResp successRestResp(Object data) {
        RestResp restResp = new RestResp();
        restResp.setData(data);
        restResp.setCode(CODE_SUCCESS);
        restResp.setMsg("服务调用成功");
        restResp.setDetails("");
        return restResp;
    }

    /**
     * 通用服务调用失败
     *
     * @param details
     * @return
     */
    public static RestResp failureRestResp(String details) {
        RestResp restResp = new RestResp();
        restResp.setData("");
        restResp.setCode(CODE_FAIL_B);
        restResp.setMsg("服务调用失败");
        restResp.setDetails(details);
        return restResp;
    }


    /**
     * 自定义服务调用返回结果
     *
     * @param code    200:成功；9999:服务调用失败；10001:token认证失败；10002：参数错误；10004:用户未注册
     * @param msg     执行结果信息
     * @param details 详细信息
     * @return
     */
    public static RestResp customRestResp(String code, String msg, String details) {
        RestResp restResp = new RestResp();
        restResp.setCode(code);
        restResp.setMsg(msg);
        restResp.setDetails(details);
        return restResp;
    }

    public static <T> RestResp<T> ok(T data) {
        RestResp<T> restResp = new RestResp<>();
        restResp.setData(data);
        restResp.setCode(CODE_SUCCESS);
        restResp.setMsg("服务调用成功");
        restResp.setDetails("");
        return restResp;
    }

    public static <T> RestResp<T> execute(Supplier<T> supplier) {
        try {
            T entity = supplier.get();
            return successRestResp(entity);
        } catch (ApplicationValidationException ex) {
            logger.log(Level.WARNING, String.format("The request is invalid"));
            return customRestResp(CODE_FAIL_P, "请求错误", ex.toString());
        } catch (ApplicationDomainException ex) {
            logger.log(Level.WARNING, String.format("Exception raised REST Call"));
            return customRestResp(CODE_FAIL_B, "请求失败", ex.toString());
        } catch (ApplicationInfrastructureException ex) {
            logger.log(Level.SEVERE, String.format("Fatal exception raised REST Call"));
            return customRestResp(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()),
                    "服务器报错", ex.toString());
        }
    }

    public static RestResp<?> execute(Runnable runnable) {
        try {
            runnable.run();
            return successRestResp(null);
        } catch (ApplicationValidationException ex) {
            logger.log(Level.WARNING, ex.getMessage(), ex);
            return customRestResp(CODE_FAIL_P, "请求错误", ex.getMessage());
        } catch (ApplicationDomainException ex) {
            logger.log(Level.WARNING, ex.getMessage(), ex);
            return customRestResp(CODE_FAIL_B, "请求失败", ex.getMessage());
        } catch (ApplicationInfrastructureException ex) {
            logger.log(Level.SEVERE, ex.getMessage(), ex);
            return customRestResp(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()),
                    "服务器报错", ex.getMessage());
        }
    }

}
